package org.emet.dao;

import org.emet.entity.biz.BusinessTeam;
import org.emet.jee.core.dao.Page;
import org.emet.jee.core.dao.arch.ArchDataSourceBaseDao;
import org.emet.service.dto.BusinessTeamCriteria;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository
public class BusinessTeamDao extends ArchDataSourceBaseDao<BusinessTeam, Long> {
	public Page<BusinessTeam> search(Page<BusinessTeam> page, BusinessTeamCriteria criteria) {
		Criteria c = getSession().createCriteria(BusinessTeam.class);
		NonEmptyValueHashMap<String, Object> propertyNameValues = new NonEmptyValueHashMap<String, Object>();
		propertyNameValues.put("code", criteria.getCode());
		propertyNameValues.put("name", criteria.getName());
		propertyNameValues.put("leader", criteria.getLeader());
		c.add(Restrictions.allEq(propertyNameValues));
		page.setOrderBy("id");
		page.setOrder("desc");
		return findPage(page, c);
	}

	public boolean isExists(BusinessTeam entity) {
		Long count = 0L;
		if (entity.getId() != null) {
			count = findUnique("select count(*) from BusinessTeam where id<>? and (code = ? or name = ?)",
					entity.getId(), entity.getCode(), entity.getName());
		} else {
			count = findUnique("select count(*) from BusinessTeam where  (code = ? or name = ?)", entity.getCode(),
					entity.getName());
		}
		return count > 0;
	}
}
